package org.demo2.springmvc_demo2.controller;

import org.demo2.springmvc_demo2.entity.User;
import org.demo2.springmvc_demo2.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
    @Autowired
    LoginService loginService;


    //访问login方法，使用url： http://localhost:8080/login，跳转到login.html页面
    @RequestMapping("/login")
    public String openLoginPage() {

        return "login";//把"login"字符串返回给视图解析器解析thymeleaf，解析后返回到login.html页面
    }

    //方法参数使用实体类
    @RequestMapping(value = "/login2", method = RequestMethod.POST)
    public ModelAndView login2(User user, ModelAndView mv) {
        System.out.println("username:" + user.getUsername() + ",password:" + user.getPassword());
        if (loginService.login(user) != null) {
            mv.setViewName("redirect:/");
            return mv;
        }
        //如果登陆失败，需要返回提示信息和重新返回到login.html页面
        mv.addObject("msg", "登陆失败");//设置返回的数据
        mv.setViewName("login");
        return mv;
    }
}
